<?php

namespace app\rent\controller\Product;

use think\Controller;

class Product extends Controller
{
    protected $ProductModel = null;

    public function _initialize()
    {
        $this->ProductModel = model('Product.Product');
    }

    public function index()
    {
        // 获取分页页码
        $page = $this->request->param('page', 1, 'trim');

        // 获取搜索关键字
        $keyword = $this->request->param('keyword', '', 'trim');

        // 每页显示数据
        $limit = 6;

        $start = ($page - 1) * $limit;

        $where = [
            'rentstatus' => ['<>', 1],
            'status' => ['=', 1]
        ];

        if ($keyword !== '') {
            $where['name'] =  ['like', "%$keyword%"];
        }

        $ProductData = $this->ProductModel->where($where)->order('createtime DESC')->limit($start, $limit)->select();

        // exit($this->ProductModel->getLastSql());

        if ($ProductData) {
            $this->success('查询数据成功', null, $ProductData);
        } else {
            $this->error('暂无数据', null, []);
        }
    }

    public function info()
    {
        $proid = $this->request->param('proid', '', 'trim');
        $busid = $this->request->param('busid', 0, 'trim');

        $where = [
            'proid' => $proid,
            'busid' => $busid
        ];

        $Collection = model('Business.Collection')->where($where)->find();

        $Collection = $Collection ? true : false;

        $ProductData = $this->ProductModel->where('id', $proid)->find();

        $data = [
            'Collection' => $Collection,
            'info' => $ProductData
        ];

        if ($ProductData) {
            $this->success('查询数据成功', null, $data);
        } else {
            $this->error('暂无数据', null, []);
        }
    }

    public function collection()
    {
        $busid = $this->request->param('busid', 0, 'trim');
        $proid = $this->request->param('proid', 0, 'trim');

        // $category = $this->CategoryModel->find($cateid);

        // if (!$category) {
        //     $this->error('文章不存在');
        // }

        // $Business = model('Business.Business')->find($busid);

        // if (!$Business) {
        //     $this->error('非法登录', null, ['action' => 'login']);
        // }

        // 组装条件
        $where = [
            'proid' => $proid,
            'busid' => $busid
        ];

        $collection = model('Business.Collection')->where($where)->find();

        // 提示信息
        $msg = '';

        $result = null;

        $status = false;

        if ($collection) {
            // 在收藏表能找到数据，说明已经收藏过 => 取消收藏
            $result = $collection->delete();

            $msg = '取消收藏';
        } else {
            // 说明没有收藏过 => 收藏
            $data = [
                'proid' => $proid,
                'busid' => $busid,
            ];

            $result = model('Business.Collection')->validate('common/Business/Collection')->data($data)->save();
            // exit(model('Business.Collection')->getLastSql());
            $msg = '收藏';

            $status = true;
        }

        if ($result === FALSE) {
            $this->error($msg . '失败');
        } else {
            $this->success($msg . '成功', null, ['status' => $status]);
        }
    }
}
